package com.honour.controller.interfaced;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.honour.service.interfaced.ServerInManager;
import com.honour.service.interfaced.impl.AdvertisementInService;
import com.system.controller.base.BaseController;
import com.system.util.AppUtil;
import com.system.util.MapDistance;
import com.system.util.PageData;
import com.system.util.Tools;
import com.system.util.ToolsUtil;


/** 
 * 说明：荣耀服务
 * 创建人：zhaozhenhua
 * 创建时间：2017-11-23
 */
@Controller
@RequestMapping(value="/appserverin")
public class ServerInController extends BaseController {
	
	@Resource(name="serverinService")
	private ServerInManager serverinService;
	
	@Resource(name="advertisementInService")
	private AdvertisementInService advertisementInService;
	
	/**
	 * 获得首页荣耀服务列表
	 * @Author zhaozhenhua
	 * @date   2017年11月15日
	 * @return
	 */
	@RequestMapping(value="/server")
	@ResponseBody
	public Object sHotel(){
		logBefore(logger, "获得首页荣耀服务列表");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SERVER", pd.getString("FKEY"))){	//检验请求key值是否合法
			 List<PageData> list = serverinService.listAll(pd);
			 if (list.size()!=0) {
				 map.put("pd", list);
			}
			 result = (0 == list.size()) ?  "02" :  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获取荣耀服务详情列表接口
	 * @Author zhaozhenhua
	 * @date   2017年11月15日
	 * @return
	 */
	@RequestMapping(value="/serverdetaillist")
	@ResponseBody
	public Object sServer(){
		logBefore(logger, "获取荣耀服务详情列表接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		List<PageData> list=new ArrayList<>();
		PageData p=null;
		try{
			if(Tools.checkKey("SERVERDETAIL", pd.getString("FKEY"))){	//检验请求key值是否合法
			    p= serverinService.getServerById(pd);
				if (p.get("SHOPTYPE").equals("高尔夫")) {
					 list=serverinService.getServerByGolfId(p);
					 for (PageData data : list) {
						 ToolsUtil.putImgstoArray(data, "GOLFDETIMGS");
					}
				}else if(p.get("SHOPTYPE").equals("马术")){
					 list= serverinService.getServerByHorseId(p);
					 for (PageData data : list) {
						 ToolsUtil.putImgstoArray(data, "HORSEDETIMGS");
					}
				}else if(p.get("SHOPTYPE").equals("豪车")){
					 list= serverinService.getServerByLuxCarId(p);
					 for (PageData data : list) {
						 ToolsUtil.putImgstoArray(data, "LUXCARDETIMGS");
					}
				}else if(p.get("SHOPTYPE").equals("飞机")){
					 list= serverinService.getServerByPlaneId(p);
					 for (PageData data : list) {
						 ToolsUtil.putImgstoArray(data, "PLANEDETIMGS");
					}
				}else if(p.get("SHOPTYPE").equals("游艇")){
					 list= serverinService.getServerByYachtId(p);
					 for (PageData data : list) {
						 ToolsUtil.putImgstoArray(data, "YACHTDETIMGS");
					}
				}
			 if (list.size()!=0) {
				 map.put("pd", list);
			 }
			 result = (0 == list.size()) ?  "02" :  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 *	根据经纬度计算高尔夫店距离
	 * @param args
	 * @throws Exception
	 */
	public Object getGolfDistance(PageData pd){
		Map<String,String> map = new HashMap<String,String>();
		String errInfo = "success",distance="";
		try {
			PageData p = serverinService.getUserGolfDist(pd);
			distance  = MapDistance.getDistance(p.getString("GOLFLONGITUDE"),p.getString("GOLFLATITUDE"),p.getString("USERLONGITUDE"),p.getString("USERLATITUDE"));
		} catch (Exception e) {
			errInfo = "error";
		}
		map.put("result", errInfo);				//返回结果
		map.put("distance", distance);			//距离
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获得荣耀服务高尔夫列表
	 * @Author zhaozhenhua
	 * @date   2017年11月28日
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/sgolflist")
	@ResponseBody
	public Object sGolf(){
		logBefore(logger, "获得荣耀服务高尔夫列店表");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SORTHIGHGOLF", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("sDistance", pd)){	//检查参数
					//高尔夫列表
					List<PageData> list = serverinService.getListGolfAll(pd);
					for (PageData p : list) {
						p.put("HONOURUSER_ID", pd.get("HONOURUSER_ID"));
					}
					for (PageData data : list) {
						Map<String, String> m= (Map<String, String>) getGolfDistance(data);
						data.put("distance", m.get("distance"));
						//将地址修改为标准字符串
						data.put("GOLFADDRESS", data.get("GOLFADDRESS").toString().replace(",", ""));
					}
					//添加广告
					pd.put("RELATED_ID", "7");
					List<PageData> ad = advertisementInService.getAdByPosition(pd);
					map.put("ad", ad);
					map.put("pd", list);
				//返回01为添加成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 *	根据经纬度计算马术店距离
	 * @param args
	 * @throws Exception
	 */
	public Object getHorseDistance(PageData pd){
		Map<String,String> map = new HashMap<String,String>();
		String errInfo = "success",distance="";
		try {
			PageData p = serverinService.getUserHorseDist(pd);
			distance  = MapDistance.getDistance(p.getString("HORSELONGITUDE"),p.getString("HORSELATITUDE"),p.getString("USERLONGITUDE"),p.getString("USERLATITUDE"));
		} catch (Exception e) {
			errInfo = "error";
		}
		map.put("result", errInfo);				//返回结果
		map.put("distance", distance);			//距离
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获得荣耀服务马术店列表
	 * @Author zhaozhenhua
	 * @date   2017年11月28日
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/shorselist")
	@ResponseBody
	public Object sHorse(){
		logBefore(logger, "获得荣耀服务马术店列店表");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SORTHIGHHORSE", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("sDistance", pd)){	//检查参数
					//获得某家商店的综合评价
					List<PageData> list = serverinService.getListHorseAll(pd);
					for (PageData p : list) {
						p.put("HONOURUSER_ID", pd.get("HONOURUSER_ID"));
					}
					for (PageData data : list) {
						Map<String, String> m= (Map<String, String>) getHorseDistance(data);
						data.put("distance", m.get("distance"));
						//将地址修改为标准字符串
						data.put("HORSEADDRESS", data.get("HORSEADDRESS").toString().replace(",", ""));
					}
					//添加广告
					pd.put("RELATED_ID", "4");
					List<PageData> ad = advertisementInService.getAdByPosition(pd);
					map.put("ad", ad);
					map.put("pd", list);
				//返回01为添加成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 *	根据经纬度计算豪车店距离
	 * @param args
	 * @throws Exception
	 */
	public Object getLuxCarDistance(PageData pd){
		Map<String,String> map = new HashMap<String,String>();
		String errInfo = "success",distance="";
		try {
			PageData p = serverinService.getUserLuxCarDist(pd);
			distance  = MapDistance.getDistance(p.getString("LUXCARLONGITUDE"),p.getString("LUXCARLATITUDE"),p.getString("USERLONGITUDE"),p.getString("USERLATITUDE"));
		} catch (Exception e) {
			errInfo = "error";
		}
		map.put("result", errInfo);				//返回结果
		map.put("distance", distance);			//距离
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获得荣耀服务豪车店列表
	 * @Author zhaozhenhua
	 * @date   2017年11月28日
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/sluxcarlist")
	@ResponseBody
	public Object sLuxCar(){
		logBefore(logger, "获得荣耀服务豪车店列表");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SORTHIGHLUXCAR", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("sDistance", pd)){	//检查参数
					//获得某家商店的综合评价
					List<PageData> list = serverinService.getListLuxCarAll(pd);
					for (PageData p : list) {
						p.put("HONOURUSER_ID", pd.get("HONOURUSER_ID"));
					}
					for (PageData data : list) {
						Map<String, String> m= (Map<String, String>) getLuxCarDistance(data);
						data.put("distance", m.get("distance"));
						//将地址修改为标准字符串
						data.put("LUXCARADDRESS", data.get("LUXCARADDRESS").toString().replace(",", ""));
					}
					//添加广告
					pd.put("RELATED_ID", "6");
					List<PageData> ad = advertisementInService.getAdByPosition(pd);
					map.put("ad", ad);
					map.put("pd", list);
				//返回01为添加成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 *	根据经纬度计算飞机店距离
	 * @param args
	 * @throws Exception
	 */
	public Object getPlaneDistance(PageData pd){
		Map<String,String> map = new HashMap<String,String>();
		String errInfo = "success",distance="";
		try {
			PageData p = serverinService.getUserPlaneDist(pd);
			distance  = MapDistance.getDistance(p.getString("PLANELONGITUDE"),p.getString("PLANELATITUDE"),p.getString("USERLONGITUDE"),p.getString("USERLATITUDE"));
		} catch (Exception e) {
			errInfo = "error";
		}
		map.put("result", errInfo);				//返回结果
		map.put("distance", distance);			//距离
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获得荣耀服务飞机店列表
	 * @Author zhaozhenhua
	 * @date   2017年11月28日
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/splanelist")
	@ResponseBody
	public Object s(){
		logBefore(logger, "获得荣耀服务飞机店列表");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SORTHIGHPLANE", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("sDistance", pd)){	//检查参数
					//获得某家商店的综合评价
					List<PageData> list = serverinService.getListPlaneAll(pd);
					for (PageData p : list) {
						p.put("HONOURUSER_ID", pd.get("HONOURUSER_ID"));
					}
					for (PageData data : list) {
						Map<String, String> m= (Map<String, String>) getPlaneDistance(data);
						data.put("distance", m.get("distance"));
						//将地址修改为标准字符串
						data.put("PLANELONGITUDE", data.get("PLANELATITUDE").toString().replace(",", ""));
					}
					map.put("pd", list);
				//返回01为添加成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 *	根据经纬度计算游艇店距离
	 * @param args
	 * @throws Exception
	 */
	public Object getYachtDistance(PageData pd){
		Map<String,String> map = new HashMap<String,String>();
		String errInfo = "success",distance="";
		try {
			PageData p = serverinService.getUserYachtDist(pd);
			distance  = MapDistance.getDistance(p.getString("YACHTLONGITUDE"),p.getString("YACHTLATITUDE"),p.getString("USERLONGITUDE"),p.getString("USERLATITUDE"));
		} catch (Exception e) {
			errInfo = "error";
		}
		map.put("result", errInfo);				//返回结果
		map.put("distance", distance);			//距离
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获得荣耀服务游艇店列表
	 * @Author zhaozhenhua
	 * @date   2017年11月28日
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/syachtlist")
	@ResponseBody
	public Object sYacht(){
		logBefore(logger, "获得荣耀服务游艇店列表");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SORTHIGHYACHT", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("sDistance", pd)){	//检查参数
					List<PageData> list = serverinService.getListYachtAll(pd);
					for (PageData p : list) {
						p.put("HONOURUSER_ID", pd.get("HONOURUSER_ID"));
					}
					for (PageData data : list) {
						Map<String, String> m= (Map<String, String>) getYachtDistance(data);
						data.put("distance", m.get("distance"));
						//将地址修改为标准字符串
						data.put("YACHTLONGITUDE", data.get("YACHTLATITUDE").toString().replace(",", ""));
					}
					//添加广告
					pd.put("RELATED_ID", "5");
					List<PageData> ad = advertisementInService.getAdByPosition(pd);
					//根据字段查询
					List<PageData> yachtList = advertisementInService.getYachtButtonList(pd);
					map.put("yachtList", yachtList);
					map.put("ad", ad);
					map.put("pd", list);
				//返回01为添加成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获取高尔夫详情接口
	 * @Author zhaozhenhua
	 * @date   2017年11月18日
	 * @return
	 */
	@RequestMapping(value="/golfdetail")
	@ResponseBody
	public Object golfDetail(){
		logBefore(logger, "获取高尔夫详情接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("GOLFDETAIL", pd.getString("FKEY"))){	//检验请求key值是否合法
				List<PageData> list = serverinService.getByGolfDetId(pd);
				for (PageData p : list) {
					ToolsUtil.putImgstoArray(p, "GOLFDETIMGS");
				}
					if (list.size()!=0) {
						map.put("pd", list);
					}
				//返回01为添加成功并返回,02为此商铺暂无具体服务
				result = (0 == list.size()) ?  "02" :  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获取马术详情接口
	 * @Author zhaozhenhua
	 * @date   2017年11月28日
	 * @return
	 */
	@RequestMapping(value="/horsedetail")
	@ResponseBody
	public Object horseDetail(){
		logBefore(logger, "获取马术详情接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("HORSEDETAIL", pd.getString("FKEY"))){	//检验请求key值是否合法
				List<PageData> list = serverinService.getByHorseDetId(pd);
				for (PageData p : list) {
					ToolsUtil.putImgstoArray(p, "HORSEDETIMGS");
				}
					if (list.size()!=0) {
						map.put("pd", list);
					}
				//返回01为添加成功并返回,02为此商铺暂无具体服务
				result = (0 == list.size()) ?  "02" :  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获取豪车详情接口
	 * @Author zhaozhenhua
	 * @date   2017年11月28日
	 * @return
	 */
	@RequestMapping(value="/luxcardetail")
	@ResponseBody
	public Object luxCarDetail(){
		logBefore(logger, "获取豪车详情接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("LUXCARDETAIL", pd.getString("FKEY"))){	//检验请求key值是否合法
				List<PageData> list = serverinService.getByLuxCarDetId(pd);
				for (PageData p : list) {
					ToolsUtil.putImgstoArray(p, "LUXCARDETIMGS");
				}
					if (list.size()!=0) {
						map.put("pd", list);
					}
				//返回01为添加成功并返回,02为此商铺暂无具体服务
				result = (0 == list.size()) ?  "02" :  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获取飞机详情接口
	 * @Author zhaozhenhua
	 * @date   2017年11月28日
	 * @return
	 */
	@RequestMapping(value="/planedetail")
	@ResponseBody
	public Object planeDetail(){
		logBefore(logger, "获取飞机详情接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("HORSEDETAIL", pd.getString("FKEY"))){	//检验请求key值是否合法
				List<PageData> list = serverinService.getByPlaneDetId(pd);
				for (PageData p : list) {
					ToolsUtil.putImgstoArray(p, "PLANEDETIMGS");
				}
					if (list.size()!=0) {
						map.put("pd", list);
					}
				//返回01为添加成功并返回,02为此商铺暂无具体服务
				result = (0 == list.size()) ?  "02" :  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获取游艇详情接口
	 * @Author zhaozhenhua
	 * @date   2017年11月28日
	 * @return
	 */
	@RequestMapping(value="/yachtdetail")
	@ResponseBody
	public Object yachtDetail(){
		logBefore(logger, "获取游艇详情接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("YACHTDETAIL", pd.getString("FKEY"))){	//检验请求key值是否合法
				List<PageData> list = serverinService.getByYachtDetId(pd);
				for (PageData p : list) {
					ToolsUtil.putImgstoArray(p, "YACHTDETIMGS");
				}
					if (list.size()!=0) {
						map.put("pd", list);
					}
				//返回01为添加成功并返回,02为此商铺暂无具体服务
				result = (0 == list.size()) ?  "02" :  "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
}
